package vip.xiaonuo.supplier.modular.custom.customtag.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.custom.customtag.entity.SupplierCustomTag;
import vip.xiaonuo.supplier.modular.custom.customtag.mapper.SupplierCustomTagMapper;
import vip.xiaonuo.supplier.modular.custom.customtag.param.SupplierCustomTagAddParam;
import vip.xiaonuo.supplier.modular.custom.customtag.param.SupplierCustomTagEditParam;
import vip.xiaonuo.supplier.modular.custom.customtag.param.SupplierCustomTagIdParam;
import vip.xiaonuo.supplier.modular.custom.customtag.param.SupplierCustomTagPageParam;
import vip.xiaonuo.supplier.modular.custom.customtag.service.SupplierCustomTagService;

import java.util.List;

/**
 * 客户标签Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/15 17:55
 **/
@Service
public class SupplierCustomTagServiceImpl extends ServiceImpl<SupplierCustomTagMapper, SupplierCustomTag> implements SupplierCustomTagService {

    @Override
    public Page<SupplierCustomTag> page(SupplierCustomTagPageParam supplierCustomTagPageParam) {
        QueryWrapper<SupplierCustomTag> queryWrapper = new QueryWrapper<SupplierCustomTag>().checkSqlInjection();
        //数据归属
        queryWrapper.lambda().eq(SupplierCustomTag::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierCustomTagPageParam.getSortField(), supplierCustomTagPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierCustomTagPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierCustomTagPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierCustomTagPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierCustomTag::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierCustomTagAddParam supplierCustomTagAddParam) {
        SupplierCustomTag supplierCustomTag = BeanUtil.toBean(supplierCustomTagAddParam, SupplierCustomTag.class);
        //数据归属
        supplierCustomTag.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierCustomTag);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierCustomTagEditParam supplierCustomTagEditParam) {
        SupplierCustomTag supplierCustomTag = this.queryEntity(supplierCustomTagEditParam.getId());
        BeanUtil.copyProperties(supplierCustomTagEditParam, supplierCustomTag);
        this.updateById(supplierCustomTag);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierCustomTagIdParam> supplierCustomTagIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierCustomTagIdParamList, SupplierCustomTagIdParam::getId));
    }

    @Override
    public SupplierCustomTag detail(SupplierCustomTagIdParam supplierCustomTagIdParam) {
        return this.queryEntity(supplierCustomTagIdParam.getId());
    }

    @Override
    public SupplierCustomTag queryEntity(String id) {
        SupplierCustomTag supplierCustomTag = this.getById(id);
        if(ObjectUtil.isEmpty(supplierCustomTag)) {
            throw new CommonException("客户标签不存在，id值为：{}", id);
        }
        return supplierCustomTag;
    }
}
